package com.dh.bm.dao.impl;

import com.dh.bm.common.JDBCUtil;
import com.dh.bm.dao.UserDao;
import com.dh.bm.entity.User;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    @Override
    public User login(String account, String passWord) {
        // 连接数据库1
        User user = null;
        String sql = "select uid,nickname from user where account=? and password=?";
        PreparedStatement ps = JDBCUtil.getStatement(sql);
        try {
            ps.setString(1, account);
            ps.setString(2, passWord);
            ResultSet resultSet = ps.executeQuery();
            while (resultSet.next()) {
                Integer uid = resultSet.getInt(1);
                String nickname = resultSet.getString(2);
                // 构造一个user对象
                user = new User();
                user.setUid(uid);
                user.setNickName(nickname);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return user;
    }
}
